package com.ryan.order.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.order.entity.Order;
import com.ryan.order.service.OrderService;
import com.ryan.utils.JwtUtil;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-31
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 前台生成订单
     * @param routeId
     * @return
     */
    @PostMapping("/createOrder/{routeId}")
    public R createOrder(@PathVariable String routeId, HttpServletRequest request){
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        Order order = orderService.createOrder(routeId, memberId);
        return R.ok().data("orderNo", order.getOrderNo());
    }

    /**
     * 后台添加+分页查询所有的订单
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @return
     */
    @PostMapping("/getOrderList/{pageIndex}/{pageSize}")
    public R getOrderList(@PathVariable long pageIndex,
                          @PathVariable long pageSize,
                          @RequestBody(required = false) String orderNo
                          ){
        Page<Order> resultPage = orderService.getOrderList(pageIndex, pageSize, orderNo);
        return R.ok().data("list", resultPage.getRecords()).data("totalPage", resultPage.getTotal());
    }

    @GetMapping("getOrderByNo/{orderNo}")
    public R getOrderByNo(@PathVariable String orderNo){
        Order order = orderService.getOrderByNo(orderNo);
        return R.ok().data("order",order);
    }

    /**
     * 根据id获取订单信息
     * @param id
     * @return
     */
    @GetMapping("/getOrderById/{id}")
    public R getOrderById(@PathVariable String id){
        Order order = orderService.getById(id);
        return order==null?R.error().message("没有此订单"):R.ok().data("order", order);
    }

    /**
     * 后台增加订单
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public R addOrder(@RequestBody Order order){
        boolean save = orderService.save(order);
        return save?R.ok():R.error();
    }

    /**
     * 后台更新订单
     * @param order
     * @return
     */
    @PostMapping("/updateOrder")
    public R updateOrder(@RequestBody Order order){
        boolean update = orderService.updateById(order);
        return update?R.ok():R.error();
    }

    /**
     * 后台删除订单
     * @param ids
     * @return
     */
    @PostMapping("/deleteOrder")
    public R deleteOrder(@RequestBody Long[] ids){
        boolean delete = orderService.removeByIds(Arrays.asList(ids));
        return delete?R.ok():R.error();
    }

    /**
     * 根据路线id和会员id判断是否已经购买此路线
     * @param routeId
     * @param memberId
     * @return
     */
    @GetMapping("/isBuyRoute/{routeId}/{memberId}")
    public Boolean isBuyRoute(@PathVariable String routeId, @PathVariable String memberId){
        return orderService.isBuyRoute(routeId, memberId);
    }

    /**
     * 前台页面获取订单列表
     * @return
     */
    @GetMapping("/getOrderList")
    public R getOrderList(){
        List<Order> list = orderService.list();
        return R.ok().data("list",list);
    }

    /**
     * 通过id删除订单
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id){
        boolean delete = orderService.removeById(id);
        return delete?R.ok():R.error();
    }

}

